/*
 * Copyright (C) 2008 Shane Isbell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.atom.parser;

import org.kxml2.io.KXmlParser;
import org.slideme.atom.parser.entity.AtomEntity;
import org.slideme.atom.parser.entity.AtomEntityContext;
import org.slideme.atom.parser.entity.AtomEntityHandler;
import org.slideme.atom.parser.entity.IllegalEntity;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;


public class Summary {
   private String type;

    private AtomEntity atomEntity;

    public static Summary marshalFrom(KXmlParser parser, AtomEntityContext ctx) throws IOException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }
        Summary summary = new Summary();
        try {
            parser.require(XmlPullParser.START_TAG, null, "summary");
            String type = ParserUtils.getMimeTypeForTag(parser);
            summary.setType(type);
            AtomEntityHandler atomEntityHandler = ctx.getEntityHandlerFor(type);
            if (atomEntityHandler == null) {
                parser.skipSubTree();
                summary.setAtomEntity(new IllegalEntity());
            } else {
                summary.setAtomEntity(atomEntityHandler.marshalFrom(parser));
            }
        } catch (XmlPullParserException e) {
            throw new IOException("SLIDE-000-010:" + e.toString());
        }
        return summary;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public AtomEntity getAtomEntity() {
        return atomEntity;
    }

    public void setAtomEntity(AtomEntity atomEntity) {
        this.atomEntity = atomEntity;
    }
}
